package com.common.util.utils;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Process;
import android.text.TextUtils;

import java.util.Iterator;

/**
 * @文件名称：AppUtils.java
 * @文件作者：ljb
 * @文件描述：App信息工具类
 * @版本： 1.0.0
 * @创建时间：2017-12-19 16:23
 * @修改历史：2017-12-19 16:23
 */

public class AppUtils {

    public static PackageInfo getPackageInfo() {
        Context context = ApplicationManager.getInstance().getContext();
        PackageInfo pi = null;

        try {
             PackageManager pm = context.getPackageManager();
             pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi;
    }

    public static String getVersionName() {
        PackageInfo pi = getPackageInfo();
        if (pi != null) {
            return pi.versionName;
        }

        return "";
    }

    public static int getVersionCode() {
        PackageInfo pi = getPackageInfo();
        if (pi != null) {
            return pi.versionCode;
        }
        return 1;
    }

    public static String getPackageName() {
        String packageName = "com.default.app.name";
        PackageInfo pi = getPackageInfo();
        if (pi != null && !TextUtils.isEmpty(pi.packageName)) {
            packageName = pi.packageName;
        }

        return packageName;
    }

    public static String getAppNametoPath(Context context) {
        String appName = "default_app_name";

        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            String packageName = packageInfo.packageName;
            if(!TextUtils.isEmpty(packageName)) {
                appName = packageName.replaceAll("\\.", "_");
            }
        } catch (PackageManager.NameNotFoundException var4) {
        }

        return appName;
    }

    /**
     * 获取app名称
     * @return
     */
    public static String getApplicationLabel() {
        String label = null;

        try {
            Context context = ApplicationManager.getInstance().getContext();
            PackageManager pm = context.getPackageManager();
            ApplicationInfo ai = null;
            ai = pm.getApplicationInfo(context.getPackageName(), 0);
            label = (String) pm.getApplicationLabel(ai);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return label;
    }

    public static boolean isInMainProcess() {
        Application application = ApplicationManager.getInstance().getApplication();
        String processName = application.getApplicationInfo().processName;
        String currentProcessName = getCurrentProcessName(application);
        return currentProcessName == null ? false : processName.equals(currentProcessName);
    }

    private static String getCurrentProcessName(Application application) {
        int pid = Process.myPid();
        ActivityManager am = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
        Iterator iterator = am.getRunningAppProcesses().iterator();
        ActivityManager.RunningAppProcessInfo runningAppProcessInfo = null;
        do {
            if (!iterator.hasNext()) {
                return null;
            }
            runningAppProcessInfo = (ActivityManager.RunningAppProcessInfo) iterator.next();

        }while(runningAppProcessInfo.pid != pid);

        return runningAppProcessInfo.processName;
    }

}
